<template>
	<view>
		<van-card v-for="item in goodsList" :key="item.goods_id" :num="item.goods_number"
			:price="item.goods_price | toFixed2" :title="item.goods_name" :thumb="item.goods_small_logo || defaultSrc"
			:thumb-link="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`" />
	</view>
</template>

<script>
	import {
		getGoods
	} from '../../api/goods';
	import toast from '@/utils/toast.js'
	export default {
		data() {
			return {
				queryObj: {
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				goodsList: [],
				total: '',
				defaultSrc: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
				loading: false
			};
		},
		onLoad(options) {
			console.log(options)
			this.queryObj.cid = options.cid || '5'
			this.getGoods()
		},
		methods: {
			async getGoods() {
				try {
					this.loading = true
					uni.showLoading({
						title: '加载中...'
					})
					const res = await getGoods(this.queryObj)
					console.log(res)
					this.goodsList = [...this.goodsList, ...res.message.goods]
					this.total = res.message.total
				} catch (e) {
					//TODO handle the exception
				} finally {
					this.loading = false
					uni.hideLoading()
				}
			}
		},
		onReachBottom() {
			if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return toast({
				title: '数据到底了'
			})
			if (this.loading) return
			this.queryObj.pagenum++
			this.getGoods()
		},
		async onPullDownRefresh() {
			this.queryObj.pagenum = 1
			this.goodsList = []
			this.loading = false

			await this.getGoods()
			uni.stopPullDownRefresh()
		}
	}
</script>

<style lang="scss">

</style>
